<?php

class ProductsController extends AdminController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionList() {
        $model = new ecom_products('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['ecom_products']))
            $model->attributes = $_GET['ecom_products'];

        $this->render('list', array(
            'model' => $model,
        ));
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new ecom_products;

        if (isset($_POST['ecom_products'])) {
            
            //hanya berlaku untuk product voucher
            if($_POST['ecom_products']['products_products_category_id'] == '10' ){
                $profit = $_POST['ecom_products']['products_price'] - $_POST['ecom_products']['products_hpp'];
                $bonus_value = ( $profit * $_POST['ecom_products']['products_profit_share'] ) / 100 ;
                $model->products_bonus_value = $bonus_value;
            }
            
            $model->attributes = $_POST['ecom_products'];

            /* penanganan image */
            /*
            $image_file = CUploadedFile::getInstance($model, 'products_images');
            // has the user uploaded a new file?
            if ($image_file) {
                if (!is_dir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images)) {
                    mkdir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images);
                    chmod(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images, 0777);
                }
                $model->products_images = $image_file->name;
                //$picture_file->SaveAs(Yii::app()->getBasePath().'/images/news' . $picture_name);
                $image_file->SaveAs(Yii::getPathOfAlias('webroot') . '/images/products/' . $image_file->name);
            }

             */

            $nama_file = UploadFileHelper::upload_file($fieldname='products_images', $path = '/images/products/', $model);

            if ($nama_file) {
              $model->products_images = (isset($nama_file)) ? $nama_file : '';
            }
            
            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Data has been saved successfully.');
                $this->redirect(array('list'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        if (isset($_POST['ecom_products'])) {
            $model->attributes = $_POST['ecom_products'];
            
            if($_POST['ecom_products']['products_products_category_id'] == '10' ){
                $profit = $_POST['ecom_products']['products_price'] - $_POST['ecom_products']['products_hpp'];
                $bonus_value = ( $profit * $_POST['ecom_products']['products_profit_share'] ) / 100 ;
                $model->products_bonus_value = $bonus_value;
            }

            /* penanganan image */
            /*
            $image_file = CUploadedFile::getInstance($model, 'products_images');
            // has the user uploaded a new file?
            if ($image_file) {
                if (!is_dir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images)) {
                    mkdir(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images);
                    chmod(Yii::getPathOfAlias('webroot') . '/images/products/' . $model->products_images, 0777);
                }
                $model->products_images = $image_file->name;
                //$picture_file->SaveAs(Yii::app()->getBasePath().'/images/news' . $picture_name);
                $image_file->SaveAs(Yii::getPathOfAlias('webroot') . '/images/products/' . $image_file->name);
            }
            */
            $nama_file = UploadFileHelper::upload_file($fieldname='products_images', $path = '/images/products/', $model);

            if ($nama_file) {
              $model->products_images = (isset($nama_file)) ? $nama_file : '';
            }

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Data has been saved successfully.');
                $this->redirect(array('list'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();
            
            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }
    
    public function loadModel($id) {
        $model = ecom_products::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}